package zoe.app.start.controller;

import cn.hutool.core.thread.ThreadUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import zoe.app.start.service.AsyncService;

import java.util.concurrent.ExecutionException;

/**
 * @author zoe.Zhang
 * @date 2022/12/29
 * @description
 */
@RestController
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class SyncMethod {

    private final AsyncService asyncService;

    // http://localhost:8080/syncMethod
    @GetMapping("/syncMethod")
    public String asyncService() {
        System.out.println("111");
        ThreadUtil.sleep(10 * 1000); // 线程睡眠 100 秒,浏览器发送请求后,如果浏览器把请求终止了,服务器代码还是会继续往下执行
        return asyncService.syncMethod();
    }

    // http://localhost:8080/thisCallAsync
    @GetMapping("/thisCallAsync")
    public String thisCallAsync() {
        return asyncService.thisCallAsync(); // thisAsync调用失效，参考事务
    }

    // http://localhost:8080/aopCallAsync
    @GetMapping("/aopCallAsync")
    public String aopCallAsync() {
        return asyncService.aopCallAsync();
    }

    // http://localhost:8080/asyncMethod
    @GetMapping("/asyncMethod")
    public String asyncMethod() {
        return asyncService.asyncMethod();
    }

    // http://localhost:8080/asyncMethodBad
    @GetMapping("/asyncMethodBad")
    public String asyncMethodBad() throws ExecutionException, InterruptedException {
        return asyncService.syncMethodBatchBad();
    }

    // http://localhost:8080/asyncMethodGood
    @GetMapping("/asyncMethodGood")
    public String asyncMethodGood() throws ExecutionException, InterruptedException {
        return asyncService.syncMethodBatchGood();
    }
}
